package com.lw.yygh.hosp.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.yygh.exception.YyghException;
import com.lw.yygh.hosp.service.HospSetService;
import com.lw.yygh.model.hosp.HospSet;
import com.lw.yygh.result.Result;
import com.lw.yygh.result.ResultCodeEnum;
import com.lw.yygh.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;

@RestController
@RequestMapping("/admin/hosp/hospSet")
public class HospSetController {
    @Autowired
    private HospSetService hospSetService;
    //查询所有医院信息
    @GetMapping("/findAll")
    public Result findAll(){
        List<HospSet> list = hospSetService.list();
        return Result.ok(list);
    }
    //逻辑删除医院信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") String id){
        boolean removeById = hospSetService.removeById( id );
        if (removeById){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //条件查询带分页 根据医院名称或者医院编码
    @GetMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Integer current,
                                  @PathVariable("limit") Integer limit,
                                  @RequestParam Map<String,Object> map){
        String hosname= (String) map.get( "hosname" );
        String hoscode= (String) map.get( "hoscode" );
        Page<HospSet> page=new Page<>( current,limit );
        QueryWrapper<HospSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty( hoscode )){
            queryWrapper.eq( "hoscode",hoscode );
        }
        if (!StringUtils.isEmpty( hosname )){
            queryWrapper.like( "hosname",hosname );//模糊查询 ，自动添加%
        }
        Page<HospSet> hospSetPage = hospSetService.page( page, queryWrapper );
        return Result.ok(hospSetPage);
    }
    //添加医院
    @PostMapping("/saveHospSet")
    public Result saveHospSet(@RequestBody HospSet hospSet){
        //设置状态
        hospSet.setStatus( 0 );
        //设置签名密钥
        Random random=new Random();
        hospSet.setSignKey( MD5.encrypt( System.currentTimeMillis()+""+random.nextInt(1000) ) );
        //调用service
        boolean flag = hospSetService.save( hospSet );
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id获取医院设置
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id){
        return Result.ok( hospSetService.getById( id ) );
    }
    //修改医院信息
    @PostMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospSet hospSet){
        boolean flag = hospSetService.updateById( hospSet );
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //批量删除医院
    @DeleteMapping("/batchRemove")
    public Result batchRemove( @RequestBody List<Integer> ids){
        boolean flag = hospSetService.removeByIds( ids ) ;
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //医院设置的锁定和解锁
    @PutMapping("/lock/{id}/{status}")
    public Result lock (@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        //根据id查询医院设置信息
        HospSet hospSet = hospSetService.getById( id );
        //设置状态
        hospSet.setStatus( status );
        //修改
        hospSetService.updateById( hospSet );
        return Result.ok();
    }
    //发送签名密钥
    @GetMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable("id") Integer id){
        HospSet hospSet = hospSetService.getById( id );
        String signKey = hospSet.getSignKey();
        String hoscode = hospSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
